import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;


public class MailBox {
	
	static String numero;
	private static int oldMex = 0;
	private static int newMex = 0;
	private static CodeMessaggio coda = new CodeMessaggio();
	
	
	
public static boolean controlloNumero(String phoneNumber){
		File f = new File("./resource/" +phoneNumber + ".txt");
		if (f.exists()){
			numero = phoneNumber;
			return true;
		}else
			return false;
	}

public static boolean controlloCodice (String code) throws IOException{
	File f = new File("./resource/" + numero + ".txt");
	LineNumberReader rd = new LineNumberReader(new FileReader(f));
	String riga = rd.readLine();
	if(code.equals(riga))
		return true;
	else
		return false;
	
}
	
//2 metodi uno per la lettura dell'array vecchi old mex e l'altro per newmex
public static void scritturaFile()throws Exception{
	//aggiungere codice
		try{
		FileWriter f0 = new FileWriter("./resource/" + numero + ".txt"); //scrivere sullo stesso file di lettura
		f0.write("Number: " + numero + "\n" + "MexRead: " + oldMex + "\n" + "MexNew: " + newMex + "\n");
		for (int i=0; i < oldMex; i++) {
			f0.write(coda.mexRead.get(i));
			f0.write("@\n");
		}
		
		for (int i=0; i < newMex; i++) {
			f0.write(coda.mexNew.get(i));
			f0.write("@\n");
		}
		f0.close();
		}catch(IOException e){
			System.out.println(e);
		}
	}
public static void scritturaFileNewMex()throws Exception{
	
	File f = new File("./resource/" + numero + ".txt");
	
	String newMexWriting = "lakds\n";

	LineNumberReader rd = new LineNumberReader(new FileReader(f));
	
	
	
	FileWriter f0 = new FileWriter("./resource/" + numero + ".txt", true);
	
	//f0.write();
	f0.write(newMexWriting);
	f0.write("@\n");
	f0.close();		
	
}
}
